<template>
  <div class="attack-defense-form">
    <div class="title">{{ typestr }}设置</div>
    <el-form class="form" ref="form" :model="info" :rules="rules" label-width="80px">
      <el-form-item :label="`选择${typestr}`" prop="name">
        <el-select v-model="info.name">
          <el-option
            v-for="(_, name) in methods"
            :key="name"
            :value="name"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="`${typestr}参数`"/>
    </el-form>
    <el-form v-if="info.name" class="indent" label-width="150px">
      <el-form-item
        v-for="(_, name) in methods[info.name]"
        :key="name"
        :label="name">
        <typed-input v-model="methods[info.name][name]"/>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import TypedInput from './TypedInput'

export default {
  components: {
    TypedInput
  },
  props: {
    typestr: String
  },
  data() {
    return {
      methods: {},
      info: {
        name: ''
      },
      rules: {
        name: [{ required: true, message: `请选择${this.typestr}`, trigger: 'change' }]
      }
    }
  },
  methods: {
    validate() {
      return this.$refs.form.validate()
    },
    submit() {
      return {
        method: this.info.name,
        config: { ...this.methods[this.info.name] }
      }
    },
    loadTemplate(methods) {
      this.methods = methods
    },
    loadConfig(config) {
      this.info.name = config.method
      const thisConfig = this.methods[config.method]
      for (const key in thisConfig) {
        thisConfig[key] = config.config[key]
      }
    }
  }
}
</script>

<style scoped>
.form {
  margin-top: 20px;
}

.indent {
  margin-left: 50px;
}
</style>
